﻿@model IEnumerable<TaskManager.Domain.BusinessObjects.Project>
@using MvcContrib.UI.Grid
@using MvcContrib.Pagination;
@using TaskManager.Web.Extensions

<asp:content id="Content2" contentplaceholderid="head" runat="server">
  <script src="@Url.Content("~/Scripts/Grid.js")" type="text/javascript"></script>
</asp:content>

<script type="text/javascript">
    $(function () {
        EnableAjaxForGrid('projectList');
    });
</script>

@helper PrintTasks(TaskManager.Domain.BusinessObjects.Project project)
{
    int totalCount = 0;

    if (project.Tasks != null)
    {
        totalCount = project.Tasks.Count();
    }

    if (totalCount == 0)
    {
        <text>None</text>
    }
    else
    {
        int activeTasks = project.Tasks.Count(task => task.State == TaskManager.Domain.BusinessObjects.TaskState.InProgress);
        
        <text>@activeTasks.ToString() active from @totalCount.ToString()</text>
    }
}

@{
    Html.Grid(Model)
        .Sort(ViewBag.sort as GridSortOptions)
        .Attributes(@class => "zebra-striped")
        .Empty("There are no projects available")
        .Columns(column =>
        {
            column.For(project => Html.ActionLink(project.Name, "Details", new { id = project.Id })).Named("Name");
            column.For(project => project.Description).Named("Description").Attributes(style => "width:350px").Sortable(false);
            column.For(project => PrintTasks(project)).Named("Tasks").Attributes(style => "width:150px").Sortable(false);
            column.For(project => Html.DisplayFor(modelItem => project.State)).Named("State").Sortable(false);
        }).Render();  
}

@Html.Pager((IPagination)Model, 10, (linkText, routeValues) => Html.ActionLink(linkText, "Index", routeValues).ToString())